package com.imooc.framework.thread;

import java.util.concurrent.locks.ReentrantLock;

public class ReentrantLockDemo implements Runnable {

    private static ReentrantLock lock = new ReentrantLock(false);
    @Override
    public void run() {
        while (true){
            try {
                lock.lock();
                System.out.println(Thread.currentThread().getName() + "  get lock");
                Thread.sleep(1000);
                System.out.println(Thread.currentThread().getName() + "  get lock second");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } finally {
                lock.unlock();
            }
        }
    }

    public static void main(String[] args) {
        ReentrantLockDemo reentrantLockDemo = new ReentrantLockDemo();
        ReentrantLockDemo reentrantLockDemo1 = new ReentrantLockDemo();
        Thread t1 = new Thread(reentrantLockDemo);
        Thread t2 = new Thread(reentrantLockDemo1);
        t1.setName("lock1");
        t2.setName("lock2");
        t1.start();
        t2.start();
    }
}
